package com.gold.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gold.common.StringUtils;
import com.gold.dao.CardInfoDao;
import com.gold.entity.CardInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author XiaoHao
 * @date 2022-05-10 13:59
 */
@Service
@Slf4j
public class CardInfoService {
    @Autowired
    CardInfoDao cardInfoDao;

    /**
     * 生成卡密
     * @param cardInfo
     */
    public void insert(CardInfo cardInfo){
        Integer days = 7;
        switch (cardInfo.getType()) {
            case "W":
                days = 7;
                break;
            case "M":
                days = 30;
                break;
            case "S":
                days = 90;
                break;
            case "Y":
                days = 365;
                break;
            default:
                days = 7;
        }

        //生成几个
        Integer number = cardInfo.getNumber();
        if (number<0){
            return;
        }
        List<CardInfo> list = new ArrayList<>();
        Date date = new Date();
        long l = System.currentTimeMillis();
        //生成批量插入的对象集合
        while (number>0){
            number--;
            CardInfo cardInfo1 = new CardInfo();
            cardInfo1.setCreateTime(date);
            cardInfo1.setStatus("0");
            cardInfo1.setName(getName(days));
            cardInfo1.setDays(days);
            cardInfo1.setType(cardInfo.getType());
            list.add(cardInfo1);
        }
        cardInfoDao.insertList(list);
        long l1 = System.currentTimeMillis();
        long l2 = l1 - l;
        log.info("新增卡密成功,个数：{} 所用时间：{} ms",cardInfo.getNumber(),l2);
    }



    public void update(CardInfo cardInfo){
        cardInfoDao.updateById(cardInfo);
    }
    public void delete(CardInfo cardInfo){
        cardInfoDao.deleteById(cardInfo.getId());
    }

    /**
     * 根据名称查询
     * @param name
     * @return
     */
    public CardInfo selectName(String name){
        QueryWrapper<CardInfo> qw = new QueryWrapper<>();
        qw.eq("name",name);
        return cardInfoDao.selectOne(qw);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public CardInfo selectId(String id){
        return cardInfoDao.selectById(id);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    public IPage<CardInfo> selectAll(Integer page,Integer size,CardInfo cardInfo){
        Page<CardInfo> page1 = new Page<>(page, size);
        QueryWrapper<CardInfo> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(cardInfo.getType())){
            qw.eq("type",cardInfo.getType());
        }
        if (StringUtils.isNotEmpty(cardInfo.getStatus())){
            qw.eq("status",cardInfo.getStatus());
        }
        IPage<CardInfo> cardInfoIPage = cardInfoDao.selectPage(page1, qw);
        return cardInfoIPage;
    }


    /**
     * 生成卡密名称
     * @param day
     * @return
     */
    public static String getName(int day){
        String base="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder();
        sb.append("GD");
        sb.append(day);
        Random random = new Random();
        int i =0;
        while (i<10){
            sb.append(base.charAt(random.nextInt(base.length())));
            i++;
        }
        return sb.toString();
    }

}
